<?php

namespace app\common\model;

use think\facade\Cache;
use think\Model;

class ConfigTagModel extends Model
{
    protected static function init()
    {
        ConfigTagModel::afterInsert(function ($user) {
            Cache::rm('tag_list');
        });
        ConfigTagModel::afterUpdate(function ($user) {
            Cache::rm('tag_list');
        });
        ConfigTagModel::afterDelete(function ($user) {
            Cache::rm('tag_list');
        });
    }

    public static function lists(): array
    {
        return static::select()->toArray();
        // return Cache::remember('tag_list', function () {
        //     return static::select()->toArray();
        // });
    }

    public static function getTitleAndPoint()
    {
        $data = [];
        foreach (static::lists() as $item) {

            if ($item['type'] == 1) {
                $point = '+' . $item['point'];
            } else {
                $point = '-' . $item['point'];
            }
            $data[$item['id']] =  [
                'select_title' => $item['title'] . " 热度值($point)",
                'title' => $item['title'],
                'color' => $item['color'],
            ];
        }

        return $data;
    }
}